#include "customer.h"
#include <QDebug>

customer::customer(const QString name):person(name) {
    discount=1;
    for (int i = 0; i < 2; ++i) {
        orders[i] = nullptr;
    }
}

customer::customer(const QString name,QString phone):person(name){
    if(phone=="1"){
        discount=0.95;
    }else if(phone=="2"){
        discount=0.9;
    }else if(phone=="3"){
        discount=0.85;
    }else{
        discount=1;
    }
    for (int i = 0; i < 2; ++i) {
        orders[i] = nullptr;
    }
}

QString customer::queryPopular() const{
    QFile file("../menu/菜品销量.txt");
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        QString line;
        QString name="";
        while (!in.atEnd()) {
            line = in.readLine();

            QStringList parts = line.split(' ');
            double result;
            double start=0;
            result=parts[1].toDouble();

            if (result>start) {
                start=result;
                name=parts[0];
            }
        }
        return name;
        file.close();
    } else {
        throw "无法打开menu销量文件!";
    }
}

void customer::startOrder(){
    i++;
    order *o=new order(this);
    orders[i]=o;
}

void customer::orderFood(QString name,int num){
    try{
        orders[i]->orderfood(name,num);

    }catch(...){
        throw "订单操作失败！！";
    }
}

QStringList customer::checkOrder(){
    QStringList myQueue=orders[i]->checkOrder();
    for (int i = 0; i < myQueue.size(); i++) {
        QString line = myQueue[i];
        qDebug()<<line;
    }
    return myQueue;
}

void customer::submitOrder(QString name){
    orders[i]->submitorder(name);
}

void customer::returnOrder(QString num){
    int k=num.toInt();
    orders[k-1]=nullptr;
    QFile::remove("../order/订单/订单" + num + ".txt");
}

void customer::orderAgain(QString man){
    i++;
    order *o1=new order(this,*orders[i-1]);
    orders[i]=o1;
    int j=0;
    while(j<o1->orders.size()){
        QString name=o1->orders[j].name;
        int num=o1->orders[j].quantity;
        o1->orderfood1(name,num);
        j++;
    }
    submitOrder(man);
}

customer::~customer(){
    delete orders[2];
}

QStringList customer::queryAll(){
    QStringList myQueue;
    QFile file("../menu/menu.txt");

    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        QString line;
        while (!in.atEnd()) {
            line = in.readLine();
            myQueue.append(line);
        }
        file.close();
        return myQueue;
    } else {
        // 文件打开失败时的处理
        QStringList emptyList;
        throw emptyList;  // 返回空的 QStringList
    }
}
